﻿package ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import components.MyButton;
import components.MyFrame;
import components.MyPanel;

import vo.salesManager.CustomerQueryPanelVO;

import main.Main;


public class CustomerQueryPanel extends MyPanel{

	private MyButton logoutButton;
	private MyButton returnbackButton;
	
	private JLabel userlistLabel;
	private JLabel usersearchLabel;
	private DefaultTableModel userdtm;
	private JTable userlist;
	private JScrollPane userjsp;
	private JTextField usersearchField;
	private JLabel customerclassLabel;
	private JComboBox customerclassComboBox;
	private MyButton usersearchButton;
	private MyButton addcustomerButton;
	private MyButton deletecustomerButton;
	private MyButton modifycustomerButton;

	protected void initComponent() {
		try {
			Main.getLogic().searchCustomer();
		} catch (RemoteException e1) {
			e1.printStackTrace();
		}
		
		CustomerQueryPanelVO cqpvo=null;
		try {
			cqpvo=Main.getLogic().getCqpvo();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
		logoutButton = new MyButton("注销");
		returnbackButton = new MyButton("返回");
		logoutButton.setBounds(MyFrame.WIDTH-80,0, 80, 20);
		returnbackButton.setBounds(MyFrame.WIDTH-160,0, 80, 20);
		this.add(logoutButton);
		this.add(returnbackButton);
		setBounds(300, 150, 700, 450);
		userlistLabel = new JLabel("用户列表：");
		usersearchLabel = new JLabel("精确查找：");
		userdtm = new DefaultTableModel(cqpvo.getUserData(),new String[]{"用户名","身份"});
		userlist = new JTable(userdtm);
		DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
		tcr.setHorizontalAlignment(SwingConstants.CENTER);
		userlist.setDefaultRenderer(Object.class, tcr);
		userjsp = new JScrollPane(userlist);
		userjsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		usersearchField = new JTextField();
		customerclassLabel = new JLabel("类别：");
		customerclassComboBox = new JComboBox(new String[]{"用户名"});
		usersearchButton = new MyButton("精确查找");
		addcustomerButton = new MyButton("增加用户");
		deletecustomerButton = new MyButton("删除用户");
		modifycustomerButton = new MyButton("修改信息");
		userlistLabel.setBounds(10, 20, 100, 20);
		usersearchLabel.setBounds(400, 20, 100, 20);
		userjsp.setBounds(10, 40, 350, 300);
		usersearchField.setBounds(390, 50, 200, 25);
		customerclassLabel.setBounds(390, 100, 80, 20);
		customerclassComboBox.setBounds(490, 100, 100, 20);
		usersearchButton.setBounds(480, 200, 100, 40);
		addcustomerButton.setBounds(385, 280, 90, 40);
		deletecustomerButton.setBounds(485, 280, 90, 40);
		modifycustomerButton.setBounds(585, 280,90, 40);
		add(userjsp);
	    add(userlistLabel);
		add(usersearchLabel);
		add(usersearchField);
		add(customerclassLabel);
		add(customerclassComboBox);
		add(usersearchButton);
		add(addcustomerButton);
		add(deletecustomerButton);
		add(modifycustomerButton);
		
		
		logoutButton.setButtonAction("logout");
		returnbackButton.setButtonAction("toMain");
		usersearchButton.setButtonAction("searchCustomer");
		addcustomerButton.setButtonAction("toCustomerAdd");
		deletecustomerButton.setButtonAction("deleteCustomer");
		modifycustomerButton.setButtonAction("toCustomerModify");
	}

	@Override
	protected void updateVO() {
		CustomerQueryPanelVO cqpvo=null;
		try {
			cqpvo = Main.getLogic().getCqpvo();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		cqpvo.setKeyword(usersearchField.getText());
		cqpvo.setKeywordType(customerclassComboBox.getSelectedIndex());
		cqpvo.setSelectedRow(userlist.getSelectedRow());
		try {
			Main.getLogic().setCqpvo(cqpvo);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	
	

}
